package com.aiwu.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.constraintlayout.widget.ConstraintLayout;

/**
 * 控制焦点不能移出
 */
public class FocusConstraintLayout extends ConstraintLayout {
    private boolean interceptFocusOut;

    public FocusConstraintLayout(Context context) {
        super(context);
    }

    public FocusConstraintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FocusConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public boolean isInterceptFocusOut() {
        return interceptFocusOut;
    }

    public void setInterceptFocusOut(boolean interceptFocusOut) {
        this.interceptFocusOut = interceptFocusOut;
    }

    @Override
    public View focusSearch(View focused, int direction) {
        if (!interceptFocusOut) {
            return super.focusSearch(focused, direction);
        }
        View targetView = super.focusSearch(focused, direction);
        if (targetView == null) {
            return focused;
        }
        if (indexOfChild(targetView) < 0) {
            //不是Child
            return focused;
        }
        return targetView;
    }
}
